﻿using Iworld.Client.Framework.AuthorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 赠送金币视图模型
    /// </summary>
    public class GiveGoldsViewModel : SubPageFrameViewModelBase
    {
        string account;
        string description;
        string golds;
        string twoLevelPassword;

        /// <summary>
        /// 二级密码
        /// </summary>
        public string TwoLevelPassword
        {
            get
            {
                return twoLevelPassword;
            }
            set
            {
                if (twoLevelPassword != value)
                {
                    twoLevelPassword = value;
                    this.OnPropertyChanged("TwoLevelPassword");
                }
            }

        }
       
        /// <summary>
        /// 赠送的金币
        /// </summary>
        public string Golds
        {
            get
            {
                return golds;
            }
            set
            {
                if (golds != value)
                {
                    golds = value;
                    this.OnPropertyChanged("Golds");
                }
            }
        }
        /// <summary>
        /// 描述
        /// </summary>
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                if (description != value)
                {
                    description = value;
                    this.OnPropertyChanged("Description");
                }
            }
        }
        /// <summary>
        /// 赠送的账号
        /// </summary>
        public string Account
        {
            get
            {
                return account;
            }
            set
            {
                if (account != value)
                {
                    account = value;
                    this.OnPropertyChanged("Account");
                }
            }
        }

        /// <summary>
        /// 提交的命令
        /// </summary>
        public UniversalCommand SubmitCommand { get; set; }


        public GiveGoldsViewModel()
            : base(PageCategory.金币管理.ToString(),Page.赠送金币.ToString())
        {

            SubmitCommand = new UniversalCommand((paramater) =>
            {
                try
                {
                    AuthorServiceClient asc = new AuthorServiceClient();
                    asc.GiveGoldsCompleted += this.WrapEventHandler<GiveGoldsCompletedEventArgs>(GiveGoldsCompleted);
                    asc.GiveGoldsAsync(new GiveGoldsImport()
                    {
                        Account = this.Account,
                        Description = this.Description,
                        Golds = double.Parse(this.Golds),
                        Token = DataManager.GetValue<string>(DataKey.Iworld_Token),
                         TwoLevelPassword=this.TwoLevelPassword
                    }
                    );
                }
                catch  
                {
                    this.ShowError("请输入正确的信息");
                }
        
            });
        }

        void GiveGoldsCompleted(object sender, GiveGoldsCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
                this.ShowMsg("赠送成功!");
            }
            else
            {
                this.ShowError(e.Result.Error);
            }
        }
    }
}
